/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <string>
#include <vector>
#include <memory>
#include <baja/db/db.h>
#include <journal/models/db/table_interfaces.h>

namespace journal { namespace model { namespace db
{

class photo_table : public iphoto_table,
                    public icommon_table_client
{
public:
    photo_table();
    static std::shared_ptr<iphoto_table> create(std::shared_ptr<s::iengine> dbEngine);

    // icommon_table_client
    void defineColumns(std::shared_ptr<s::itable> table);
    void defineIndexColumns(std::shared_ptr<s::iindex> index);
    void defineRowCells(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> row);

    // ibase_table
    std::shared_ptr<icommon_table> common();

    // iphoto_table
    void readActiveIds(
        b::uint32 pageId,
        std::vector<b::uint32>& ids);

    void dispose(
        b::uint32 pageId);

    void insert(b::uint32 photoId,
                b::uint32 pageId,
                bool active,
                b::uint32 mediaFileId,
                b::float32 scale,
                b::float32 angle,
                b::float32 x,
                b::float32 y);

    void update(b::uint32 photoId,
                b::uint32 pageId,
                bool active,
                b::uint32 mediaFileId,
                b::float32 scale,
                b::float32 angle,
                b::float32 x,
                b::float32 y);

    void load(b::uint32 photoId,
              b::uint32 pageId,
              bool& active,
              b::uint32& mediaFileId,
              b::float32& scale,
              b::float32& angle,
              b::float32& x,
              b::float32& y);

    void dispose(
        b::uint32 photoId,
        b::uint32 pageId);

    void disposeInactiveRecords();

    void getMediaFileIds(
        std::set<b::uint32>& mediaFileIds);

private:
    std::recursive_mutex _mutex;

    std::shared_ptr<icommon_table> _commonTable;
    std::weak_ptr<icommon_table_client> _commonTableClient;

    std::shared_ptr<s::icolumn_typed<b::uint32>> _photoIdColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _pageIdColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _activeColumn;
    std::shared_ptr<s::icolumn_typed<b::uint32>> _mediaFileIdColumn;
    std::shared_ptr<s::icolumn_typed<b::float32>> _scaleColumn;
    std::shared_ptr<s::icolumn_typed<b::float32>> _angleColumn;
    std::shared_ptr<s::icolumn_typed<b::float32>> _xColumn;
    std::shared_ptr<s::icolumn_typed<b::float32>> _yColumn;

    std::shared_ptr<s::icell_typed<b::uint32>> _photoIdCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _pageIdCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _activeCell;
    std::shared_ptr<s::icell_typed<b::uint32>> _mediaFileIdCell;
    std::shared_ptr<s::icell_typed<b::float32>> _scaleCell;
    std::shared_ptr<s::icell_typed<b::float32>> _angleCell;
    std::shared_ptr<s::icell_typed<b::float32>> _xCell;
    std::shared_ptr<s::icell_typed<b::float32>> _yCell;

    // PK columns
    std::shared_ptr<s::iindex_column> _pageIdIndexColumn;
    std::shared_ptr<s::iindex_column> _photoIdIndexColumn;

    // active index.
    std::shared_ptr<s::iindex> _activeIndex;
    std::shared_ptr<s::iindex_column> _activeIndexColumn;

    void read(b::uint32 photoId, 
              b::uint32 pageId,
              std::shared_ptr<s::iresult> result);
};

std::shared_ptr<journal::model::db::iphoto_table> createPhotoTable(std::shared_ptr<s::iengine> dbEngine);

}}} // journal::model::db